package org.java.item.web;

import org.java.common.vo.ResultVo;
import org.java.item.entity.Sku;
import org.java.item.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author WHQ
 * @version 1.0
 * @date 2022-3-4 0004 下午 3:07
 */
@RestController
public class SkuController {

    @Resource
    private SkuService skuService;

    /**
     * 查询所有商品的方法
     * 网关访问地址:http://api.shopping.com/item/query/list?page=1&limit=5
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/query/list")
    public ResponseEntity<ResultVo<Sku>> getList(@RequestParam("page")int page,@RequestParam("limit")int limit){
        System.out.println("进来了");
        ResultVo<Sku> vo = skuService.getList(page, limit);
        return ResponseEntity.ok(vo);
    }

    /**
     * 网关访问地址:http://api.shopping.com/item/sku/del/{skuId}
     * @param skuId
     * @return
     */
    @DeleteMapping("/sku/del/{skuId}")
    public ResponseEntity<Void> del(@PathVariable("skuId")Long skuId){
        System.out.println(skuId);
        skuService.del(skuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改商品详情的方法
     * 访问地址:http://api.shopping.com/item/sku/update
     * @param sku
     * @return
     */
    @PatchMapping("/sku/update")
    public ResponseEntity<Void> update(Sku sku){
        System.out.println("修改商品信息的方法");
        skuService.update(sku);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 添加商品的方法
     * @param sku
     * @return
     */
    @PostMapping("/sku/save")
    public ResponseEntity<Void> saveSku(Sku sku){
        System.out.println("进入添加商品的方法");
//        System.out.println(sku.getId());
        skuService.saveSku(sku);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/query/all")
    public ResponseEntity<ResultVo<Sku>> getListAll() throws IOException {
        System.out.println("进来了");
        ResultVo<Sku> listAll = skuService.getListAll();
        return ResponseEntity.ok(null);
    }
    @GetMapping("/find/{id}")
    public ResponseEntity<Sku> findById(@PathVariable("id")Long id){
        Sku sku=skuService.findById(id);
        return ResponseEntity.ok(sku);
    }

    /**
     * 减少库存的方法
     * @param skuId
     * @return
     */
    @PostMapping("/cut/inventory")
    public ResponseEntity<Void> cutInventory(Long skuId,Integer num){
        skuService.cutInventory(skuId,num);
        return ResponseEntity.ok().build();
    }
    @PutMapping("/rollback/inventory")
    public ResponseEntity<Void> rollbackInventory(Long skuId,Long inventory){
        skuService.rollbackInventory(skuId,inventory);
        return ResponseEntity.ok().build();
    }
}
